package cn.cqone.crm.controller;

import cn.cqone.crm.dto.IdsDto;
import cn.cqone.crm.service.BaseService;
import cn.cqone.crm.utils.JSONResult;
import cn.cqone.crm.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

@ResponseBody
@CrossOrigin
public class BaseController<T,ID extends Serializable,Q> {

    @Autowired
    private BaseService<T,ID,Q> baseService;

    //查询全部数据
    @RequestMapping(value = "/all",method = RequestMethod.GET)
    public JSONResult findAll(){
        try{
            List<T> list = baseService.findAll();
            return JSONResult.ok(list);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("服务器异常,我们正在殴打程序员...");
        }
    }

    //根据id查询数据
    //查询全部数据
    @RequestMapping(value = "/findById/{id}",method = RequestMethod.GET)
    public JSONResult findById(@PathVariable("id") ID id){
        try{
            T t = baseService.findById(id);
            return JSONResult.ok(t);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("服务器异常,我们正在殴打程序员...");
        }
    }

    //@RequestBody:接收josn数据的
    //分页查询
    //查询全部数据
    @RequestMapping(value = "/findByPage",method = RequestMethod.POST)
    public JSONResult selectForPage(@RequestBody  Q q){
        try{
            PageList<T> pageList = baseService.findByPage(q);
            return JSONResult.ok(pageList);
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("服务器异常,我们正在殴打程序员...");
        }
    }


    //新增
    //@RequestBody :用来接收JSON数据
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public JSONResult add(@RequestBody  T t){
        try{
            baseService.add(t);
            return JSONResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("服务器异常,我们正在殴打程序员...");
        }
    }
    //删除 http://localhost/dept/delete/1
    // @PathVariable("id") :把url后面的 参数值，赋值给 id 变量
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") ID id){
        try{
            int delete = baseService.delete(id);
            if (delete==-1){
                return JSONResult.error("你删不了不该删的东西");
            }
            return JSONResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("服务器异常,我们正在殴打程序员...");
        }
    }

    //批量删除
    @RequestMapping(value = "/batchDelete",method = RequestMethod.POST)
    public JSONResult delete(@RequestBody IdsDto dto){
        try{
            baseService.batchDelete(dto.getIds());
            return JSONResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("服务器异常,我们正在殴打程序员...");
        }
    }

    //修改
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public JSONResult update(@RequestBody T t){
        try{
            baseService.update(t);
            return JSONResult.ok();
        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.error("服务器异常,我们正在殴打程序员...");
        }
    }
}
